clear all

******************************************************************************************
******************************************************************************************
* The individual-level data from the Norwegian Election Surveys employed here is 
* available from the  * Norwegian Center for Research, NSD free of charge. Researchers 
* fill out an application form (see https://nsd.no/nsd/english/orderform.html), and 
* will be provided access subject to signing a confidentiality agreement.
******************************************************************************************
******************************************************************************************


* Calculate the environmental indicator with a common scale
gen     Miljo=natur    if aar==2001 | aar==2005 | aar==2009 | aar==2013
* In years prior to 2001, the scale is reversed
replace Miljo=10-natur if aar==1997
* In addition, in the years 1989 and 1993 the scale goes from 1-10 (as opposed to from 0 to 10). 
* A zero represents a "don't know" answer.
replace natur=. if natur==0 & aar<=1993
replace Miljo=10-natur if aar==1993 | aar==1989

********************************************************************************************************************************
* NOTE. Pairs involving 1997 survey are dropped from all analysis. The scale was reversed uptil 1997, respondents in both      *
* the 1997 and the 2001 surveys have faced 10 as both a negative and positive statement about environmental preferences. Data  *
* is probably contaminated by this issue! The years 1989 and 1993 had questions with a 10 point scale, as opposed to 11 points *
* in 2001 and onwards, so respondents in both 1993 and 1997 faced (slightly) different scales.								   *

********************************************************************************************************************************
* NOTE: id is the respondent identification number for a given survey. y is the identification of survey-year pairs. 		   *
* The calculation of idy=id/y ensures that we have a unique respondent identification in each pair of the rotating survey.     *
* dup is a variable that identifies duplicaties - we want those observations where idy occurs two times, i.e. dup=1 and dup=2. * 
* We also calculate lagged variables for age and "kvinne" to check that pairs of observations have the same gender, and that   *
* the age-difference is about 4 years. We also accept 3 and 5 year age differences since it can be a little more and less than *
* 4 years between the national elections.                                                                                      * 
********************************************************************************************************************************

* Calculate respondent income in 2013-prices, 2013=100
gen     Inntekt=inntekt*1.00000 if aar==2013
replace Inntekt=inntekt*1.06762 if aar==2009
replace Inntekt=inntekt*1.16594 if aar==2005
replace Inntekt=inntekt*1.23459 if aar==2001
replace Inntekt=inntekt*1.37219 if aar==1997
replace Inntekt=inntekt*1.48124 if aar==1993
replace Inntekt=inntekt*1.66916 if aar==1989
replace Inntekt=. if Inntekt>9000	   

* Generate lagged values of all relavent variables
gen idy=id/y
sort idy aar
quietly by idy:  gen dup = cond(_N==1,0,_n)
by idy: gen kvinnelag1= kvinne[_n-1]
by idy: gen alderlag1 = alder[_n-1]
by idy: gen Inntektlag1 = inntekt[_n-1]
by idy: gen Miljolag1   = Miljo[_n-1]
by idy: gen hvlag1      = hv[_n-1]
by idy: gen utdanninglag1=utdanning[_n-1]
by idy: gen klimalag1=klima[_n-1]
by idy: gen ipolitikklag1=ipolitikk[_n-1]
by idy: gen vmiljolag1=vmiljo[_n-1]
replace Inntektlag1=. if Inntektlag1>9000	   

* Generate differenced values of all relavent variables
gen DMiljo=Miljo-Miljolag1             if dup==2 & Miljolag1!=.
gen DUtdanning=utdanning-utdanninglag1 if dup==2 & utdanninglag1!=.
gen DInntekt=inntekt-Inntektlag1       if dup==2 & Inntektlag1!=.
gen Dalder=alder-alderlag1             if dup==2 & alderlag1>0
gen Dkvinne=kvinne-kvinnelag1          if dup==2 & kvinnelag1!=.
gen Dhv=hv-hvlag1                      if dup==2 & hvlag1!=.
gen Dklima=klima-klimalag1             if dup==2 & klimalag1!=.
gen Dipolitikk=ipolitikk-ipolitikklag1 if dup==2 & ipolitikklag1!=.
gen Dvmiljo=vmiljo-vmiljolag1           if dup==2 & vmiljolag1!=.

* Removing duplicate observations from the dataset
drop if dup==0 | dup>2 | dup==.
gen A=dup-1
drop if Dalder==48
bysort idy: gen counter = _N if Miljo>=0 & Inntekt>=0 & utdanning>=0 & A>=0
drop if counter==1

*********************************************************************************************************************************
* NOTE: Missing values must be taken into account in the regressions to ensure that we have "complete pairs".                   *
* Mmiss, Imiss and Umiss identifies missing values in the pairs. Miss=0 ensure that we have complete pairs for all the relevant *
* variables in the environment regression.                                                                                      * 
*********************************************************************************************************************************

*tabmiss Miljo dup A alder Inntekt utdanning aar idy fodselsaar
egen Mmiss =  max(missing(Miljo))    , by(idy)
egen Imiss =  max(missing(Inntekt))  , by(idy)
egen Umiss =  max(missing(utdanning)), by(idy)
egen Amiss =  max(missing(alder))    , by(idy)
gen Miss=0 if Mmiss==0 & Imiss==0 & Umiss==0 & Amiss==0
tab Miss
drop if id19891993 ==663 | id20012005 ==667

* Dropping all pairs involving 1997 survey (due to scale reversal and scale extension around that year, see above)
drop if y==19972001 | y==19931997

* Control 1: Controlling the structure of the dataset without excluding missing values
format y %12.0f
tab y aar
* Control 2: Controlling the structure of the dataset after excluding missing values
format y %12.0f
tab y aar if Miss==0 

* Labeling key variables
lab var alder "Age"
lab var Miljo "Environmental preference"
lab var y "Panel indicator"
lab var idy "Respondent identity"
lab var aar "Year"

replace Inntekt=Inntekt/100
lab var Inntekt "Income, 100.000 NOK, 2013=100"
lab var utdanning "Education level"
lab var A "Period indicator (=1)"
label var hv     "Left-right self-placement"
label var ipolitikk "Immigration policy"
label var vmiljo "Too little emphasis on environmenal issues"
label var klima "Climate change a major problem"

* Generate age groups
gen     AA=1 if             alder<30
replace AA=2 if alder>=30 & alder<40
replace AA=3 if alder>=40 & alder<50
replace AA=4 if alder>=50 & alder<60
replace AA=5 if alder>=60 
label def AAdef 1 "<30" 2 "30-40" 3 "40-50" 4 "50-60" 5 "60-"
label val AA AAdef


log using "C:\Users\A1010571\Dropbox\Miljoanalyser 31.01 2017\Data & analysis\Miljoresultater_20180214.txt", text replace


* MAIN RESULTS
*-------------------------------------------------------------------------------
* Dalder==4 always imposed as these are mismatched pairs with different birthyears!!

* Main regression (Environmental preferences on the first difference with detrended data a la Cheng, Powdthavee and Oswald (2015) EJ 
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar>1997
predict RDMiljo, res
reg RDMiljo alder if Miss==0 & Dalder==4 & aar>1997, vce(robust)
reg RDMiljo i.AA  if Miss==0 & Dalder==4 & aar>1997, vce(robust) 

* Graphical illustration of the main result
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar>1997
predict RDMiljo, res
qui cmogram RDMiljo alder  if Miss==0 & Dalder==4 & aar>1997, histopts(width(1)) scatter lfitci note graphopts(yline(0, lpattern(dash)) ytitle("Detrended within-person preference change") xtitle("Age")  xlabel(20(10)90, labsize(vsmall)) msize(small) ) 
graph save Figure2.gph, replace

drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar~=1997
predict RDMiljo, res
qui cmogram RDMiljo alder  if Miss==0 & Dalder==4 & aar~=1997, histopts(width(1)) scatter lfitci note graphopts(yline(0, lpattern(dash)) ytitle("Detrended within-person preference change") xtitle("Age")  xlabel(20(10)90, labsize(vsmall)) msize(small) ) 
graph save Figure2_full.gph, replace


* APPENDIX 
*-------------------------------------------------------------------------------
* Descriptive statistics
*-------------------------------------------------------------------------------
* Environmental preferences
binscatter Miljo aar, line(connect) xtitle("Election year") ytitle("Preference indicator") note("Source: Norwegian Election Surveys 1989-2013.")


* Describing the frequencies of environmental preference shifts between elections
twoway histogram DMiljo if Miss==0 & Dalder==4 & aar>1997, discrete percent fcolor(black*0.6) color(white) xtitle("Preference shifts") graphregion(color(white)) saving(Figure3.gph, replace) note("Source: Norwegian Election Surveys 2001-2013.")
twoway histogram DMiljo if Miss==0 & Dalder==4 & aar~=1997, discrete percent fcolor(black*0.6) color(white) xtitle("Preference shifts") graphregion(color(white)) saving(Figure3_full.gph, replace) note("Source: Norwegian Election Surveys 1989-2013.")

 
* Main regression: robustness
*-------------------------------------------------------------------------------

* Adding demographic controls
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar>1997
predict RDMiljo, res
reg RDMiljo alder kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar>1997, vce(robust)
reg RDMiljo i.AA  kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar>1997, vce(robust) 

* Excluding extreme shifts (+10 and -10)
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-10 & DMiljo<10
predict RDMiljo, res
reg RDMiljo alder if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-10 & DMiljo<10, vce(robust)
reg RDMiljo i.AA  if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-10 & DMiljo<10, vce(robust)
reg RDMiljo alder kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-10 & DMiljo<10, vce(robust)
reg RDMiljo i.AA  kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-10 & DMiljo<10, vce(robust) 

* Excluding extreme shifts (+9 and -9)
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-9 & DMiljo<9
predict RDMiljo, res
reg RDMiljo alder if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-9 & DMiljo<9, vce(robust)
reg RDMiljo i.AA  if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-9 & DMiljo<9, vce(robust)
reg RDMiljo alder kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-9 & DMiljo<9, vce(robust)
reg RDMiljo i.AA  kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar>1997 & DMiljo>-9 & DMiljo<9, vce(robust) 


* Including 1989-1993 surveys
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar~=1997 
predict RDMiljo, res
reg RDMiljo alder if Miss==0 & Dalder==4 & aar~=1997 , vce(robust)
reg RDMiljo i.AA  if Miss==0 & Dalder==4 & aar~=1997 , vce(robust) 
reg RDMiljo alder kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar~=1997, vce(robust)
reg RDMiljo i.AA  kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar~=1997, vce(robust)
 
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-10 & DMiljo<10
predict RDMiljo, res
reg RDMiljo alder if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-10 & DMiljo<10, vce(robust)
reg RDMiljo i.AA  if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-10 & DMiljo<10, vce(robust)
reg RDMiljo alder kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-10 & DMiljo<10, vce(robust)
reg RDMiljo i.AA  kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-10 & DMiljo<10, vce(robust)
 
drop RDMiljo
reg DMiljo i.aar if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-9 & DMiljo<9
predict RDMiljo, res
reg RDMiljo alder if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-9 & DMiljo<9, vce(robust)
reg RDMiljo i.AA  if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-9 & DMiljo<9, vce(robust)
reg RDMiljo alder kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-9 & DMiljo<9, vce(robust)
reg RDMiljo i.AA  kvinne Inntekt utdanning if Miss==0 & Dalder==4 & aar~=1997 & DMiljo>-9 & DMiljo<9, vce(robust)


* Other regression models (Note that these regressions are not well identified)
* Note that the slope - the coefficient for the effect of a change in age - can be identified by calculating differences in coefficients.

* Standard first difference regression (without detrending the data)
reg DMiljo alder i.aar if Miss==0 & Dalder==4 & aar>1997, vce(robust)

* Fixed effect regression, with detrending the data on the level of environmental preferences
xtset idy
xtreg Miljo i.aar if Miss==0 & Dalder==4 & aar>1997, fe vce(cluster idy)
predict Miljo_res, res
reg Miljo_res alder if Miss==0 & Dalder==4 & aar>1997, vce(robust)


*--------------------------------------------------
* Robustness check using alternative specifications 
*--------------------------------------------------

* IDEA: Respondents are classified into five-year age-groups and five-year birth cohorts.
* NOTE: With individual FE, this can still identify moves from one age-group to another independent of the cohort and time effects
sort idy aar
replace Dalder=Dalder[_n+1] if Dalder==.
xtset idy A
xtsum Miljo DMiljo alder Inntekt utdanning if Miss==0 & aar~=1997 & Dalder==4
replace DMiljo=DMiljo[_n+1] if DMiljo==.

* Figure describing cross-sectional evidense for a hump-shaped relationship between environmental preferences and age
xtset idy A
qui cmogram Miljo alder  if Miss==0 & Dalder==4 & aar>1997, histopts(width(1)) scatter qfitci note graphopts(xtitle("Age")  xlabel(20(10)90, labsize(vsmall)) msize(small) )
graph save Figure1.gph, replace

* With 1989-1993 samples included
qui cmogram Miljo alder  if Miss==0 & Dalder==4 & aar~=1997, histopts(width(1)) scatter qfitci note graphopts(xtitle("Age")  xlabel(20(10)90, labsize(vsmall)) msize(small) ) 
graph save Figure1_full.gph, replace


****** 5-year age groups and birth cohorts

* Generate age groups
gen     AgeGroup=1 if            alder<=25
replace AgeGroup=2 if alder>25 & alder<=30
replace AgeGroup=3 if alder>30 & alder<=35
replace AgeGroup=4 if alder>35 & alder<=40
replace AgeGroup=5 if alder>40 & alder<=45
replace AgeGroup=6 if alder>45 & alder<=50
replace AgeGroup=7 if alder>50 & alder<=55
replace AgeGroup=8 if alder>55 & alder<=60
replace AgeGroup=9 if alder>60 & alder<=65
replace AgeGroup=10 if alder>65 & alder<=70
replace AgeGroup=11 if alder>70 & alder~=.

label var AgeGroup "Five-year age-groups"

* Generate 5-year birth cohorts
gsort -y id20092013 aar
replace fodselsaar =fodselsaar[_n-1] if aar==2013 & id20092013==id20092013[_n-1]
drop if Dalder~=4

gen     Cohort=1 if             fodselsaar<=1925
replace Cohort=2 if fodselsaar>1925 & fodselsaar<=1930
replace Cohort=3 if fodselsaar>1930 & fodselsaar<=1935
replace Cohort=4 if fodselsaar>1935 & fodselsaar<=1940
replace Cohort=5 if fodselsaar>1940 & fodselsaar<=1945
replace Cohort=6 if fodselsaar>1945 & fodselsaar<=1950
replace Cohort=7 if fodselsaar>1950 & fodselsaar<=1955
replace Cohort=8 if fodselsaar>1955 & fodselsaar<=1960
replace Cohort=9 if fodselsaar>1960 & fodselsaar<=1965
replace Cohort=10 if fodselsaar>1965 & fodselsaar<=1970
replace Cohort=11 if fodselsaar>1970 & fodselsaar<=1975
replace Cohort=12 if fodselsaar>1975 & fodselsaar<=1980
replace Cohort=13 if fodselsaar>1980 & fodselsaar~=.

label var Cohort "Five-year cohorts"

* Results

xtset idy aar
xtreg Miljo i.AgeGroup i.Cohort i.aar if Miss==0 & aar~=1997, fe vce(cluster idy)
xtreg Miljo i.AgeGroup i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)

xtreg Miljo i.AgeGroup i.Cohort i.aar if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)
xtreg Miljo i.AgeGroup i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)

tab AgeGroup, gen(Group)
label var Group1 "<25"
label var Group2 "26-30"
label var Group3 "31-35"
label var Group4 "36-40"
label var Group5 "41-45"
label var Group6 "46-50"
label var Group7 "51-55"
label var Group8 "56-60"
label var Group9 "61-65"
label var Group10 "66-70"
label var Group11 ">70"

xtreg Miljo Group2-Group11 i.Cohort i.aar kvinne Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)
coefplot , keep(Group*) levels(90) vertical ytitle("Coefficient point estimate") mstyle(p1) mcolor(black) ciopts(lcolor(black)) saving(Figure4_5y.gph, replace)

gen alder2=alder*alder
xtreg Miljo alder alder2 i.Cohort i.aar if Miss==0 & aar~=1997, fe vce(cluster idy)
xtreg Miljo alder alder2 i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)

xtreg Miljo alder alder2 i.Cohort i.aar if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)
xtreg Miljo alder alder2 i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)

* detrend first version
reg Miljo i.aar if Miss==0 & aar~=1997
predict RMiljo, res
xtreg RMiljo Group2-Group11 i.Cohort kvinne Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)
coefplot , keep(Group*) levels(90) vertical ytitle("Coefficient point estimate") mstyle(p1) mcolor(black) ciopts(lcolor(black)) saving(Figure4_5y_detrend.gph, replace)


****** 10 year age groups and birth cohorts
drop AgeGroup Cohort Group*

* Generate age groups
gen     AgeGroup=1 if            alder<=25
replace AgeGroup=2 if alder>25 & alder<=35
replace AgeGroup=3 if alder>35 & alder<=45
replace AgeGroup=4 if alder>45 & alder<=55
replace AgeGroup=5 if alder>55 & alder<=65
replace AgeGroup=6 if alder>65 & alder~=.

label var AgeGroup "Ten-year age-groups"

* Generate birth cohorts
gen     Cohort=1 if             fodselsaar<=1930
replace Cohort=2 if fodselsaar>1930 & fodselsaar<=1940
replace Cohort=3 if fodselsaar>1940 & fodselsaar<=1950
replace Cohort=4 if fodselsaar>1950 & fodselsaar<=1960
replace Cohort=5 if fodselsaar>1960 & fodselsaar<=1970
replace Cohort=6 if fodselsaar>1970 & fodselsaar<=1980
replace Cohort=7 if fodselsaar>1980 & fodselsaar~=.

label var Cohort "Ten-year cohorts"

* Results

xtset idy aar
xtreg Miljo i.AgeGroup i.Cohort i.aar if Miss==0 & aar~=1997, fe vce(cluster idy)
xtreg Miljo i.AgeGroup i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)

xtreg Miljo i.AgeGroup i.Cohort i.aar if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)
xtreg Miljo i.AgeGroup i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)

tab AgeGroup, gen(Group)
label var Group1 "<25"
label var Group2 "26-35"
label var Group3 "36-45"
label var Group4 "46-55"
label var Group5 "56-65"
label var Group6 ">65"

xtreg Miljo Group2-Group6 i.Cohort i.aar kvinne Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)
coefplot , keep(Group*) levels(90) vertical ytitle("Coefficient point estimate") mstyle(p1) mcolor(black) ciopts(lcolor(black)) saving(Figure4_10y.gph, replace)

xtreg Miljo alder alder2 i.Cohort i.aar if Miss==0 & aar~=1997, fe vce(cluster idy)
xtreg Miljo alder alder2 i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)

xtreg Miljo alder alder2 i.Cohort i.aar if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)
xtreg Miljo alder alder2 i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)

* detrend first version
xtreg RMiljo Group2-Group6 i.Cohort kvinne Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)
coefplot , keep(Group*) levels(90) vertical ytitle("Coefficient point estimate") mstyle(p1) mcolor(black) ciopts(lcolor(black)) saving(Figure4_10y_detrend.gph, replace)

xtreg RMiljo alder i.Cohort i.aar if Miss==0 & aar~=1997, fe vce(cluster idy)
xtreg RMiljo alder i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)

xtreg RMiljo alder i.Cohort i.aar if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)
xtreg RMiljo alder i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)

xtreg RMiljo alder alder2 i.Cohort i.aar if Miss==0 & aar~=1997, fe vce(cluster idy)
xtreg RMiljo alder alder2 i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997, fe vce(cluster idy)

xtreg RMiljo alder alder2 i.Cohort i.aar if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)
xtreg RMiljo alder alder2 i.Cohort i.aar Inntekt utdanning if Miss==0 & aar~=1997 & DMiljo>-9 & DMiljo<9, fe vce(cluster idy)


log close
